/**
 * 作者: wgx
 * 日期: 2024-10-17 16:06:31
 * 备注: 用户经营资产表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserAffair;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserAffairModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected long partnerExp; // 干员经验 (用来升级干员)
    protected int monsterTideBuff; // 兽潮战斗产生的buff (0-无变化1-增加收益,2-减少收益)
    protected long monsterTideBuffStime; // 兽潮战斗产生的buff的开始时间
    protected long earnTime; // 上次结算收益的时间
    protected long resetTime; // 重置时间
    protected String worship = ""; // 排行榜膜拜列表
    protected int monsterTideBuffValue; // 兽潮战斗产生的buff加成百分比
    protected int mainTaskId; // 主线任务的id
    protected int buddyGiftNum; // 好友赠送次数
    protected int buddyGetGiftNum; // 领取好友赠送次数
    protected String systemOpenReward = ""; // 系统模块的解锁奖励的领取记录
    protected String systemOpen = ""; // 已解锁的系统模块id 列表 (记录下来，避免重复计算)
    protected int secrFaviroteMax; // 秘书最高亲密度
    protected int secrFaviroteTotal; // 秘书总的亲密度
    protected long offlineGoldNum; // 离线收益的金币数量 (观看广告可以再获得1份)
    protected int isMonsterGuide; // 是否已经领取了兽潮引导奖励 (1-已经领取)

    public void reset() {
    }

    protected String getKey() {
        return "UserAffair." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_affair"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("partnerExp", partnerExp); // 干员经验 (用来升级干员)
        obj.put("monsterTideBuff", monsterTideBuff); // 兽潮战斗产生的buff (0-无变化1-增加收益,2-减少收益)
        obj.put("monsterTideBuffStime", monsterTideBuffStime); // 兽潮战斗产生的buff的开始时间
        obj.put("earnTime", earnTime); // 上次结算收益的时间
        obj.put("resetTime", resetTime); // 重置时间
        obj.put("worship", worship); // 排行榜膜拜列表
        obj.put("monsterTideBuffValue", monsterTideBuffValue); // 兽潮战斗产生的buff加成百分比
        obj.put("mainTaskId", mainTaskId); // 主线任务的id
        obj.put("buddyGiftNum", buddyGiftNum); // 好友赠送次数
        obj.put("buddyGetGiftNum", buddyGetGiftNum); // 领取好友赠送次数
        obj.put("systemOpenReward", systemOpenReward); // 系统模块的解锁奖励的领取记录
        obj.put("systemOpen", systemOpen); // 已解锁的系统模块id 列表 (记录下来，避免重复计算)
        obj.put("secrFaviroteMax", secrFaviroteMax); // 秘书最高亲密度
        obj.put("secrFaviroteTotal", secrFaviroteTotal); // 秘书总的亲密度
        obj.put("offlineGoldNum", offlineGoldNum); // 离线收益的金币数量 (观看广告可以再获得1份)
        obj.put("isMonsterGuide", isMonsterGuide); // 是否已经领取了兽潮引导奖励 (1-已经领取)
        return obj;
    }

    public PBUserAffair.Builder takeInitBuilder() {
        PBUserAffair.Builder sb = PBUserAffair.newBuilder();
        if (partnerExp != 0) {
            sb.setPartnerExp(partnerExp);
        }
        if (monsterTideBuff != 0) {
            sb.setMonsterTideBuff(monsterTideBuff);
        }
        if (monsterTideBuffStime != 0) {
            sb.setMonsterTideBuffStime(monsterTideBuffStime);
        }
        if (this.worship != null && this.worship.length() > 0) {
            JsonArray list = new JsonArray(this.worship);
            for(int i=0; i<list.size(); i++) {
                sb.addWorship(list.getInteger(i));
            }
        }
        if (monsterTideBuffValue != 0) {
            sb.setMonsterTideBuffValue(monsterTideBuffValue);
        }
        if (mainTaskId != 0) {
            sb.setMainTaskId(mainTaskId);
        }
        if (buddyGiftNum != 0) {
            sb.setBuddyGiftNum(buddyGiftNum);
        }
        if (buddyGetGiftNum != 0) {
            sb.setBuddyGetGiftNum(buddyGetGiftNum);
        }
        if (this.systemOpenReward != null && this.systemOpenReward.length() > 0) {
            JsonArray list = new JsonArray(this.systemOpenReward);
            for(int i=0; i<list.size(); i++) {
                sb.addSystemOpenReward(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserAffair.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserAffair.Builder sb = PBUserAffair.newBuilder();
        if (isFieldUpdate("partnerExp")) {
            sb.setPartnerExp(partnerExp);
        }
        if (isFieldUpdate("monsterTideBuff")) {
            sb.setMonsterTideBuff(monsterTideBuff);
        }
        if (isFieldUpdate("monsterTideBuffStime")) {
            sb.setMonsterTideBuffStime(monsterTideBuffStime);
        }
        if (isFieldUpdate("worship")) {
            if (this.worship != null && this.worship.length() > 0) {
                JsonArray list = new JsonArray(this.worship);
                for(int i=0; i<list.size(); i++) {
                    sb.addWorship(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearWorship();
                }
            } else {
                sb.clearWorship();
            }
        }
        if (isFieldUpdate("monsterTideBuffValue")) {
            sb.setMonsterTideBuffValue(monsterTideBuffValue);
        }
        if (isFieldUpdate("mainTaskId")) {
            sb.setMainTaskId(mainTaskId);
        }
        if (isFieldUpdate("buddyGiftNum")) {
            sb.setBuddyGiftNum(buddyGiftNum);
        }
        if (isFieldUpdate("buddyGetGiftNum")) {
            sb.setBuddyGetGiftNum(buddyGetGiftNum);
        }
        if (isFieldUpdate("systemOpenReward")) {
            if (this.systemOpenReward != null && this.systemOpenReward.length() > 0) {
                JsonArray list = new JsonArray(this.systemOpenReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addSystemOpenReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearSystemOpenReward();
                }
            } else {
                sb.clearSystemOpenReward();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getPartnerExp() {
        return partnerExp;
    }

    public void putPartnerExp(long partnerExp) {
        if (this.partnerExp != partnerExp) {
            this.partnerExp = partnerExp;
            updateRedisHashField(getKey(), "partnerExp", this.partnerExp);
        }
    }

    public void addPartnerExp(long value) {
        this.partnerExp += value;
        updateRedisHashField(getKey(), "partnerExp", this.partnerExp);
    }

    public int getMonsterTideBuff() {
        return monsterTideBuff;
    }

    public void putMonsterTideBuff(int monsterTideBuff) {
        if (this.monsterTideBuff != monsterTideBuff) {
            this.monsterTideBuff = monsterTideBuff;
            updateRedisHashField(getKey(), "monsterTideBuff", this.monsterTideBuff);
        }
    }

    public void addMonsterTideBuff(int value) {
        this.monsterTideBuff += value;
        updateRedisHashField(getKey(), "monsterTideBuff", this.monsterTideBuff);
    }

    public long getMonsterTideBuffStime() {
        return monsterTideBuffStime;
    }

    public void putMonsterTideBuffStime(long monsterTideBuffStime) {
        if (this.monsterTideBuffStime != monsterTideBuffStime) {
            this.monsterTideBuffStime = monsterTideBuffStime;
            updateRedisHashField(getKey(), "monsterTideBuffStime", this.monsterTideBuffStime);
        }
    }

    public void addMonsterTideBuffStime(long value) {
        this.monsterTideBuffStime += value;
        updateRedisHashField(getKey(), "monsterTideBuffStime", this.monsterTideBuffStime);
    }

    public long getEarnTime() {
        return earnTime;
    }

    public void putEarnTime(long earnTime) {
        if (this.earnTime != earnTime) {
            this.earnTime = earnTime;
            updateRedisHashField(getKey(), "earnTime", this.earnTime);
        }
    }

    public void addEarnTime(long value) {
        this.earnTime += value;
        updateRedisHashField(getKey(), "earnTime", this.earnTime);
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public JsonArray getWorshipArray() {
        if (this.worship != null && this.worship.length() > 0) {
            return new JsonArray(this.worship);
        } else {
            return new JsonArray();
        }
    }

    public String getWorship() {
        return worship;
    }

    public void putWorship(String worship) {
        if (!this.worship.equals(worship)) {
            this.worship = worship;
            updateRedisHashField(getKey(), "worship", this.worship);
        }
    }

    public int getMonsterTideBuffValue() {
        return monsterTideBuffValue;
    }

    public void putMonsterTideBuffValue(int monsterTideBuffValue) {
        if (this.monsterTideBuffValue != monsterTideBuffValue) {
            this.monsterTideBuffValue = monsterTideBuffValue;
            updateRedisHashField(getKey(), "monsterTideBuffValue", this.monsterTideBuffValue);
        }
    }

    public void addMonsterTideBuffValue(int value) {
        this.monsterTideBuffValue += value;
        updateRedisHashField(getKey(), "monsterTideBuffValue", this.monsterTideBuffValue);
    }

    public int getMainTaskId() {
        return mainTaskId;
    }

    public void putMainTaskId(int mainTaskId) {
        if (this.mainTaskId != mainTaskId) {
            this.mainTaskId = mainTaskId;
            updateRedisHashField(getKey(), "mainTaskId", this.mainTaskId);
        }
    }

    public void addMainTaskId(int value) {
        this.mainTaskId += value;
        updateRedisHashField(getKey(), "mainTaskId", this.mainTaskId);
    }

    public int getBuddyGiftNum() {
        return buddyGiftNum;
    }

    public void putBuddyGiftNum(int buddyGiftNum) {
        if (this.buddyGiftNum != buddyGiftNum) {
            this.buddyGiftNum = buddyGiftNum;
            updateRedisHashField(getKey(), "buddyGiftNum", this.buddyGiftNum);
        }
    }

    public void addBuddyGiftNum(int value) {
        this.buddyGiftNum += value;
        updateRedisHashField(getKey(), "buddyGiftNum", this.buddyGiftNum);
    }

    public int getBuddyGetGiftNum() {
        return buddyGetGiftNum;
    }

    public void putBuddyGetGiftNum(int buddyGetGiftNum) {
        if (this.buddyGetGiftNum != buddyGetGiftNum) {
            this.buddyGetGiftNum = buddyGetGiftNum;
            updateRedisHashField(getKey(), "buddyGetGiftNum", this.buddyGetGiftNum);
        }
    }

    public void addBuddyGetGiftNum(int value) {
        this.buddyGetGiftNum += value;
        updateRedisHashField(getKey(), "buddyGetGiftNum", this.buddyGetGiftNum);
    }

    public JsonArray getSystemOpenRewardArray() {
        if (this.systemOpenReward != null && this.systemOpenReward.length() > 0) {
            return new JsonArray(this.systemOpenReward);
        } else {
            return new JsonArray();
        }
    }

    public String getSystemOpenReward() {
        return systemOpenReward;
    }

    public void putSystemOpenReward(String systemOpenReward) {
        if (!this.systemOpenReward.equals(systemOpenReward)) {
            this.systemOpenReward = systemOpenReward;
            updateRedisHashField(getKey(), "systemOpenReward", this.systemOpenReward);
        }
    }

    public JsonArray getSystemOpenArray() {
        if (this.systemOpen != null && this.systemOpen.length() > 0) {
            return new JsonArray(this.systemOpen);
        } else {
            return new JsonArray();
        }
    }

    public String getSystemOpen() {
        return systemOpen;
    }

    public void putSystemOpen(String systemOpen) {
        if (!this.systemOpen.equals(systemOpen)) {
            this.systemOpen = systemOpen;
            updateRedisHashField(getKey(), "systemOpen", this.systemOpen);
        }
    }

    public int getSecrFaviroteMax() {
        return secrFaviroteMax;
    }

    public void putSecrFaviroteMax(int secrFaviroteMax) {
        if (this.secrFaviroteMax != secrFaviroteMax) {
            this.secrFaviroteMax = secrFaviroteMax;
            updateRedisHashField(getKey(), "secrFaviroteMax", this.secrFaviroteMax);
        }
    }

    public void addSecrFaviroteMax(int value) {
        this.secrFaviroteMax += value;
        updateRedisHashField(getKey(), "secrFaviroteMax", this.secrFaviroteMax);
    }

    public int getSecrFaviroteTotal() {
        return secrFaviroteTotal;
    }

    public void putSecrFaviroteTotal(int secrFaviroteTotal) {
        if (this.secrFaviroteTotal != secrFaviroteTotal) {
            this.secrFaviroteTotal = secrFaviroteTotal;
            updateRedisHashField(getKey(), "secrFaviroteTotal", this.secrFaviroteTotal);
        }
    }

    public void addSecrFaviroteTotal(int value) {
        this.secrFaviroteTotal += value;
        updateRedisHashField(getKey(), "secrFaviroteTotal", this.secrFaviroteTotal);
    }

    public long getOfflineGoldNum() {
        return offlineGoldNum;
    }

    public void putOfflineGoldNum(long offlineGoldNum) {
        if (this.offlineGoldNum != offlineGoldNum) {
            this.offlineGoldNum = offlineGoldNum;
            updateRedisHashField(getKey(), "offlineGoldNum", this.offlineGoldNum);
        }
    }

    public void addOfflineGoldNum(long value) {
        this.offlineGoldNum += value;
        updateRedisHashField(getKey(), "offlineGoldNum", this.offlineGoldNum);
    }

    public int getIsMonsterGuide() {
        return isMonsterGuide;
    }

    public void putIsMonsterGuide(int isMonsterGuide) {
        if (this.isMonsterGuide != isMonsterGuide) {
            this.isMonsterGuide = isMonsterGuide;
            updateRedisHashField(getKey(), "isMonsterGuide", this.isMonsterGuide);
        }
    }

    public void addIsMonsterGuide(int value) {
        this.isMonsterGuide += value;
        updateRedisHashField(getKey(), "isMonsterGuide", this.isMonsterGuide);
    }

}